﻿using System;
using System.Collections.Generic;

using System.Text;
using Itegracion.Framework.Forms;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;

namespace Itegracion.iAlmacen.Model
{
    public class Producto
    {
        public static List<Element> TraerListado(List<Element> entrada)
        {
            List<Element> rpta = new List<Element>();

            Database db = DatabaseFactory.CreateDatabase("iAlmacen");
            DbCommand command = db.GetSqlStringCommand(@"
                SELECT
                  p.id, p.codigo, p.nombre, p.marca, p.cantidad, p.costo, p.precio_unitario, c.nombre as tipo
                FROM
                  Producto p
                  JOIN Constante c ON c.codigo = 'TipoProducto' AND p.idTipo = c.oid
                ORDER BY
                  p.nombre");
            DataTable dt = db.ExecuteDataSet(command).Tables[0];
            rpta.Add(new Element("rpta", dt));

            return rpta;
        }
        public static List<Element> Insertar(List<Element> entrada)
        {
            List<Element> rpta = new List<Element>();

            Database db = DatabaseFactory.CreateDatabase("iAlmacen");
            DbCommand command = db.GetSqlStringCommand(@"
                INSERT INTO Producto(codigo, nombre, marca, cantidad, costo, precio_unitario, idTipo) VALUES(@codigo, @nombre, @marca, @cantidad, @costo, @precio_unitario, @idTipo)");
            db.AddInParameter(command, "codigo", DbType.String, entrada[1].Value);
            db.AddInParameter(command, "nombre", DbType.String, entrada[2].Value);
            db.AddInParameter(command, "marca", DbType.String, entrada[3].Value);
            db.AddInParameter(command, "cantidad", DbType.Decimal, entrada[4].Value);
            db.AddInParameter(command, "costo", DbType.Decimal, entrada[5].Value);
            db.AddInParameter(command, "precio_unitario", DbType.Decimal, entrada[6].Value);
            db.AddInParameter(command, "idTipo", DbType.Int32, entrada[7].Value);
            db.ExecuteNonQuery(command);
            rpta.Add(new Element("rpta", "OK"));

            return rpta;
        }
        public static List<Element> Modificar(List<Element> entrada)
        {
            List<Element> rpta = new List<Element>();

            Database db = DatabaseFactory.CreateDatabase("iAlmacen");
            DbCommand command = db.GetSqlStringCommand(@"
                UPDATE Producto SET codigo=@codigo, nombre=@nombre, marca=@marca, cantidad=@cantidad, costo=@costo, precio_unitario=@precio_unitario, idTipo=@idTipo WHERE id = @id");
            db.AddInParameter(command, "id", DbType.String, entrada[0].Value);
            db.AddInParameter(command, "codigo", DbType.String, entrada[1].Value);
            db.AddInParameter(command, "nombre", DbType.String, entrada[2].Value);
            db.AddInParameter(command, "marca", DbType.String, entrada[3].Value);
            db.AddInParameter(command, "cantidad", DbType.Decimal, entrada[4].Value);
            db.AddInParameter(command, "costo", DbType.Decimal, entrada[5].Value);
            db.AddInParameter(command, "precio_unitario", DbType.Decimal, entrada[6].Value);
            db.AddInParameter(command, "idTipo", DbType.Int32, entrada[7].Value);
            db.ExecuteNonQuery(command);
            rpta.Add(new Element("rpta", "OK"));

            return rpta;
        }
        public static List<Element> Traer(List<Element> entrada)
        {
            List<Element> rpta = new List<Element>();

            Database db = DatabaseFactory.CreateDatabase("iAlmacen");
            DbCommand command = db.GetSqlStringCommand(@"
                SELECT
                  p.id, p.codigo, p.nombre, p.marca, p.cantidad, p.costo, p.precio_unitario, p.idTipo
                FROM
                  Producto p
                WHERE
                  p.id = @id");
            db.AddInParameter(command, "id", DbType.Int32, entrada[0].Value);
            DataTable dt = db.ExecuteDataSet(command).Tables[0];
            rpta.AddRange(dt.Rows.ToList());

            return rpta;
        }
        public static List<Element> Eliminar(List<Element> entrada)
        {
            List<Element> rpta = new List<Element>();

            Database db = DatabaseFactory.CreateDatabase("iAlmacen");
            DbCommand command = db.GetSqlStringCommand(@"
                DELETE FROM Producto WHERE id = @id");
            db.AddInParameter(command, "id", DbType.Int32, entrada[0].Value);
            db.ExecuteNonQuery(command);
            rpta.Add(new Element("rpta", "OK"));

            return rpta;
        }
    }
}
